package com.erli.exchange.common.sg.mapper;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;

import com.erli.exchange.core.util.JAXBUtil;

import lombok.Setter;

//<select id="selectByEntity" parameterType="com.anban.sso.api.dao.entity.UserEntity" resultMap="BaseResultMap">
//select
//<include refid="BASE_COLUMN"/>
//FROM
//<include refid="BASE_TABLE"/>
//<where>
//    <if test="userName != null and userName != '' " >
//        and user_name = #{userName}
//    </if>
//    <if test="userNameCn != null and userNameCn != '' ">
//        and user_name_cn = #{userNameCn}
//    </if>
//    <if test="email != null and email != '' ">
//        and email = #{email}
//    </if>
//    <if test="department != null and department != '' ">
//        and department = #{department}
//    </if>
//    <if test="enabled != '' and enabled != null ">
//        and enabled = #{enabled}
//    </if>
//</where>
//</select>
@Setter
@XmlRootElement(name="select")
public class Select {

	@XmlAttribute
	private String id;
	@XmlAttribute
	private String parameterType;
	@XmlAttribute
	private String parameterMap;
	@XmlAttribute
	private String resultMap;
	
	@XmlElementRefs(value = {
		@XmlElementRef(name="include", type=Include.class),
		@XmlElementRef(name="where", type=Where.class),
	})
	@XmlMixed
	private List<Object> list;
	
	public static void main(String[] args) throws Exception {
		Select select = new Select();
		//<select id="selectByEntity" parameterType="com.anban.sso.api.dao.entity.UserEntity" resultMap="BaseResultMap">
		select.setId("selectByEntity");
		select.setParameterType("com.anban.sso.api.dao.entity.UserEntity");
		select.setResultMap("BaseResultMap");
		
		List<Object> list = new ArrayList<Object>();
		list.add("select");
		Include include1 = new Include();
		include1.setRefid("BASE_COLUMN");
		list.add(include1);
		list.add("FROM");
		Include include2 = new Include();
		include2.setRefid("BASE_COLUMN");
		list.add(include2);
		
		
		List<If> ifList = new ArrayList<>();
		If if1 = new If();
		if1.setTest("department != null and department != '' ");
		if1.setValue("and department = #{department}");
		If if2 = new If();
		if2.setTest("enabled != '' and enabled != null ");
		if2.setValue("and enabled = #{enabled}");
		ifList.add(if1);
		ifList.add(if2);
		
		Where where = new Where();
		where.setIfList(ifList);
		list.add(where);
		
		select.setList(list);
		
		System.out.println(JAXBUtil.toXML(select));
	}
}
